{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Twinkle, twinkle, little star, \n",
      "\tHow I wonder what you are! \n",
      "\t\tUp above the world so high, \n",
      "\t\tLike a diamond in the sky. \n",
      "Twinkle, twinkle, little star, \n",
      "\tHow I wonder what you are!\n"
     ]
    }
   ],
   "source": [
    "print(\"Twinkle, twinkle, little star, \\n\\tHow I wonder what you are! \\n\\t\\tUp above the world so high, \\n\\t\\tLike a diamond in the sky. \\nTwinkle, twinkle, little star, \\n\\tHow I wonder what you are!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.7.6\n"
     ]
    }
   ],
   "source": [
    "import platform\n",
    "print(platform.python_version())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前日期和时间：\n",
      "2022-03-27 13:14:46\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "now = datetime.datetime.now()\n",
    "print (\"当前日期和时间：\")\n",
    "print (now.strftime(\"%Y-%m-%d %H:%M:%S\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入半径大小: 1\n",
      "面积s为： 3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "from math import pi\n",
    "r = float(input (\"请输入半径大小: \"))\n",
    "print (\"面积s为： \"+ str(pi * r**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入姓: 张\n",
      "输入名 : 三\n",
      "你好啊  三 张\n"
     ]
    }
   ],
   "source": [
    "fname = input(\"输入姓: \")\n",
    "lname = input(\"输入名 : \")\n",
    "print (\"你好啊  \" + lname + \" \" + fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入逗号分割的序列数字: 2，3，5，4，6\n",
      "List :  ['2，3，5，4，6']\n",
      "Tuple :  ('2，3，5，4，6',)\n"
     ]
    }
   ],
   "source": [
    "values = input(\"请输入逗号分割的序列数字: \")\n",
    "list = values.split(\",\")\n",
    "tuple = tuple(list)\n",
    "print('List : ',list)\n",
    "print('Tuple : ',tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入文件名: python.py\n",
      "拓展格式为 : 'py'\n"
     ]
    }
   ],
   "source": [
    "filename = input(\"请输入文件名: \")\n",
    "f_extns = filename.split(\".\")\n",
    "print (\"拓展格式为 : \" + repr(f_extns[-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red Black\n"
     ]
    }
   ],
   "source": [
    "color_list = [\"Red\",\"Green\",\"White\" ,\"Black\"]\n",
    "print( \"%s %s\"%(color_list[0],color_list[-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入n的大小: 5\n",
      "615\n"
     ]
    }
   ],
   "source": [
    "a = int(input(\"请输入n的大小: \"))\n",
    "n1 = int( \"%s\" % a )\n",
    "n2 = int( \"%s%s\" % (a,a) )\n",
    "n3 = int( \"%s%s%s\" % (a,a,a) )\n",
    "print (n1+n2+n3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入年份: 2022\n",
      "请输入月份 : 3\n",
      "     March 2022\n",
      "Mo Tu We Th Fr Sa Su\n",
      "    1  2  3  4  5  6\n",
      " 7  8  9 10 11 12 13\n",
      "14 15 16 17 18 19 20\n",
      "21 22 23 24 25 26 27\n",
      "28 29 30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "y = int(input(\"请输入年份: \"))\n",
    "m = int(input(\"请输入月份 : \"))\n",
    "print(calendar.month(y, m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三练答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n"
     ]
    }
   ],
   "source": [
    "from datetime import date\n",
    "f_date = date(2022, 3, 2)\n",
    "l_date = date(2022, 4, 11)\n",
    "delta = l_date - f_date\n",
    "print(delta.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "体积为:  904.7786842338603\n"
     ]
    }
   ],
   "source": [
    "pi = 3.1415926535897931\n",
    "r= 6.0\n",
    "V= 4.0/3.0*pi* r**3\n",
    "print('体积为: ',V)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "def difference(n):\n",
    "    if n <= 17:\n",
    "        return 17 - n\n",
    "    else:\n",
    "        return (n - 17) * 2 \n",
    "\n",
    "print(difference(22))\n",
    "print(difference(14))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def near_thousand(n):\n",
    "      return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))\n",
    "print(near_thousand(1000))\n",
    "print(near_thousand(900))\n",
    "print(near_thousand(800))   \n",
    "print(near_thousand(2200))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "27\n"
     ]
    }
   ],
   "source": [
    "def sum_thrice(x, y, z):\n",
    "\n",
    "     sum = x + y + z\n",
    "  \n",
    "     if x == y == z:\n",
    "      sum = sum * 3\n",
    "     return sum\n",
    "\n",
    "print(sum_thrice(1, 2, 3))\n",
    "print(sum_thrice(3, 3, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "def list_count_4(nums):\n",
    "  count = 0  \n",
    "  for num in nums:\n",
    "    if num == 4:\n",
    "      count = count + 1\n",
    "\n",
    "  return count\n",
    "\n",
    "print(list_count_4([1, 4, 6, 7, 4]))\n",
    "print(list_count_4([1, 4, 6, 4, 7, 4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def is_group_member(group_data, n):\n",
    "   for value in group_data:\n",
    "       if n == value:\n",
    "           return True\n",
    "   return False\n",
    "print(is_group_member([1, 5, 8, 3], 3))\n",
    "print(is_group_member([5, 8, 3], -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15122\n"
     ]
    }
   ],
   "source": [
    "def concatenate_list_data(list):\n",
    "    result= ''\n",
    "    for element in list:\n",
    "        result += str(element)\n",
    "    return result\n",
    "\n",
    "print(concatenate_list_data([1, 5, 12, 2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入长 : 5\n",
      "请输入宽t : 3\n",
      "area =  7.5\n"
     ]
    }
   ],
   "source": [
    "b = int(input(\"请输入长 : \"))\n",
    "h = int(input(\"请输入高 : \"))\n",
    "\n",
    "area = b*h/2\n",
    "\n",
    "print(\"area = \", area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 12 & 17最大公约数 ： 1\n",
      "4 & 6 最大公约数： 2\n",
      "336 & 360最大公约数 ： 24\n"
     ]
    }
   ],
   "source": [
    "def gcd(x, y):\n",
    "   gcd = 1   \n",
    "   if x % y == 0:\n",
    "       return y   \n",
    "   for k in range(int(y / 2), 0, -1):\n",
    "       if x % k == 0 and y % k == 0:\n",
    "           gcd = k\n",
    "           break \n",
    "   return gcd\n",
    "print(\" 12 & 17最大公约数 ：\",gcd(12, 17))\n",
    "print(\"4 & 6 最大公约数：\",gcd(4, 6))\n",
    "print(\"336 & 360最大公约数 ：\",gcd(336, 360))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第五练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "255\n"
     ]
    }
   ],
   "source": [
    "def lcm(x, y):\n",
    "  if x > y:\n",
    "      z = x\n",
    "  else:\n",
    "      z = y\n",
    "  while(True):\n",
    "      if((z % x == 0) and (z % y == 0)):\n",
    "          lcm = z\n",
    "          break\n",
    "      z += 1\n",
    "  return lcm\n",
    "print(lcm(4, 6))\n",
    "print(lcm(15, 17))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "0\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def sum(x, y, z):\n",
    "    if x == y or y == z or x==z:\n",
    "        sum = 0\n",
    "    else:\n",
    "        sum = x + y + z\n",
    "    return sum\n",
    "\n",
    "print(sum(2, 1, 2))\n",
    "print(sum(3, 2, 2))\n",
    "print(sum(2, 2, 2))\n",
    "print(sum(1, 2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "12\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "def sum(x, y):\n",
    "    sum = x + y\n",
    "    if sum in range(15, 20):\n",
    "        return 20\n",
    "    else:\n",
    "        return sum\n",
    "\n",
    "print(sum(10, 6))\n",
    "print(sum(10, 2))\n",
    "print(sum(10, 12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def test_number5(x, y):\n",
    "   if x == y or abs(x-y) == 5 or (x+y) == 5:\n",
    "       return True\n",
    "   else:\n",
    "       return False\n",
    "print(test_number5(7, 2))\n",
    "print(test_number5(3, 2))\n",
    "print(test_number5(2, 2))\n",
    "print(test_number5(7, 3))\n",
    "print(test_number5(27, 53))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "请输入正确的值!\n",
      "请输入正确的值!\n",
      "请输入正确的值!\n"
     ]
    }
   ],
   "source": [
    "def add_numbers(a, b):\n",
    "   if not (isinstance(a, int) and isinstance(b, int)):\n",
    "       return \"请输入正确的值!\"\n",
    "   return a + b\n",
    "print(add_numbers(10, 20))\n",
    "print(add_numbers(10, 20.23))\n",
    "print(add_numbers('5', 6))\n",
    "print(add_numbers('5', '6'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第六练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: 张三\n",
      "Age: 19\n",
      "Address: 上海, 背景, 广东\n"
     ]
    }
   ],
   "source": [
    "def personal():\n",
    "    name, age = \"张三\", 19\n",
    "    address = \"上海, 背景, 广东\"\n",
    "    print(\"Name: {}\\nAge: {}\\nAddress: {}\".format(name, age, address))\n",
    "\n",
    "personal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4 + 3) ^ 2) = 49\n"
     ]
    }
   ],
   "source": [
    "x, y = 4, 3\n",
    "result = x * x + 2 * x * y + y * y\n",
    "print(\"({} + {}) ^ 2) = {}\".format(x, y, result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.324555320336759\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "p1 = [4, 0]\n",
    "p2 = [6, 6]\n",
    "distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )\n",
    "\n",
    "print(distance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入n的大小: 5\n",
      "输入为 5 结果为: 15.0\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入n的大小: \"))\n",
    "sum_num = (n * (n + 1)) / 2\n",
    "print(\"输入为\", n ,\"结果为:\", sum_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入最直角边的大小:\n",
      "a: 3\n",
      "b: 4\n",
      "斜边长度为: 5.0\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "print(\"请输入最直角边的大小:\")\n",
    "a = float(input(\"a: \"))\n",
    "b = float(input(\"b: \"))\n",
    "c = sqrt(a**2 + b**2)\n",
    "print(\"斜边长度为:\", c )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第七练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入天数: 4\n",
      "请输入小时: 3\n",
      "请输入分钟: 2\n",
      "请输入秒: 5\n",
      "一共秒数为： 356525\n"
     ]
    }
   ],
   "source": [
    "days = int(input(\"请输入天数: \")) * 3600 * 24\n",
    "hours = int(input(\"请输入小时: \")) * 3600\n",
    "minutes = int(input(\"请输入分钟: \")) * 60\n",
    "seconds = int(input(\"请输入秒: \"))\n",
    "\n",
    "time = days + hours + minutes + seconds\n",
    "\n",
    "print(\"一共秒数为：\", time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入秒: 45684\n",
      "d:h:m:s-> 0:12:41:24\n"
     ]
    }
   ],
   "source": [
    "time = float(input(\"请输入秒: \"))\n",
    "day = time // (24 * 3600)\n",
    "time = time % (24 * 3600)\n",
    "hour = time // 3600\n",
    "time %= 3600\n",
    "minutes = time // 60\n",
    "time %= 60\n",
    "seconds = time\n",
    "print(\"d:h:m:s-> %d:%d:%d:%d\" % (day, hour, minutes, seconds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一串四位数字: 5426\n",
      "每个数字和为 17\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入一串四位数字: \"))\n",
    "x  = num //1000\n",
    "x1 = (num - x*1000)//100\n",
    "x2 = (num - x*1000 - x1*100)//10\n",
    "x3 = num - x*1000 - x1*100 - x2*10\n",
    "print(\"每个数字和为\", x+x1+x2+x3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个数字: 4\n",
      "请输入第二个数字: 2\n",
      "请输入第三个数字: 6\n",
      "排序结果为:  2 4 6\n"
     ]
    }
   ],
   "source": [
    "x = int(input(\"请输入第一个数字: \"))\n",
    "y = int(input(\"请输入第二个数字: \"))\n",
    "z = int(input(\"请输入第三个数字: \"))\n",
    "\n",
    "a1 = min(x, y, z)\n",
    "a3 = max(x, y, z)\n",
    "a2 = (x + y + z) - a1 - a3\n",
    "print(\"排序结果为: \", a1, a2, a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "计算两点的中点 :\n",
      "x1= 2\n",
      "y1= 3\n",
      "x2=4\n",
      "y2= 5\n",
      "\n",
      "线的重点为: 3.0 4.0\n"
     ]
    }
   ],
   "source": [
    "print('\\n计算两点的中点 :')\n",
    "\n",
    "x1 = float(input('x1= '))\n",
    "y1 = float(input('y1= '))\n",
    "\n",
    "x2 = float(input('x2='))\n",
    "y2 = float(input('y2= '))\n",
    "\n",
    "x_m_point = (x1 + x2)/2\n",
    "y_m_point = (y1 + y2)/2\n",
    "print();\n",
    "print(\"线的中点为:\",x_m_point,y_m_point)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第八练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始字符串为:\n",
      "chuanchuan.\n",
      "字符串中a的个数为:\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "s = \"chuanchuan.\"  \n",
    "print(\"原始字符串为:\")\n",
    "print(s)\n",
    "print(\"字符串中a的个数为:\")\n",
    "print(s.count(\"a\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "97\n",
      "65\n",
      "49\n",
      "64\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print()\n",
    "print(ord('a'))\n",
    "print(ord('A'))\n",
    "print(ord('1'))\n",
    "print(ord('@'))\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开心的一天!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "n=1\n",
    "if n == 1:\n",
    "   print(\"开心的一天!\")\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "不是数字\n",
      "\n"
     ]
    }
   ],
   "source": [
    "str = 'a123'\n",
    "#str = '123'\n",
    "try:\n",
    "    i = float(str)\n",
    "except (ValueError, TypeError):\n",
    "    print('\\n不是数字')\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始列表为:\n",
      "['Red', 'Black', 'Green', 'White', 'Orange']\n",
      "删除第一项为:\n",
      "['Black', 'Green', 'White', 'Orange']\n"
     ]
    }
   ],
   "source": [
    "color = [\"Red\", \"Black\", \"Green\", \"White\", \"Orange\"]\n",
    "print(\"原始列表为:\")\n",
    "print(color)\n",
    "del color[0]\n",
    "print(\"删除第一项为:\")\n",
    "print(color)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第九练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字: 45\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    try:\n",
    "        a = int(input(\"请输入数字: \"))\n",
    "        break\n",
    "    except ValueError:\n",
    "        print(\"\\n这不是数字，请重试...\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始列表为：  [34, 1, 0, -23, 12, -88]\n",
      "修改后列表为:  34 1 12\n"
     ]
    }
   ],
   "source": [
    "nums = [34, 1, 0, -23, 12, -88]\n",
    "print(\"原始列表为： \",nums)\n",
    "pos_nums = [n for n in nums if n> 0]\n",
    "print(\"修改后列表为: \",*pos_nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据为:\n",
      "[10, 20, 30]\n",
      "\n",
      "乘积为: 6000\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "nums = [10, 20, 30,]\n",
    "print(\"原始数据为:\")\n",
    "print(nums)\n",
    "nums_product = reduce( (lambda x, y: x * y), nums)\n",
    "print(\"\\n乘积为:\",nums_product)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "nums = [2,2,4,6,6,8,6,10,4]\n",
    "print(len(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "str1 = 'ewgfewrgf98468I'\n",
    "print(any(c.islower() for c in str1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第十练答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red\n",
      "Green\n"
     ]
    }
   ],
   "source": [
    "d = {'Red': 'Green'}\n",
    "(c1, c2), = d.items()\n",
    "print(c1)\n",
    "print(c2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0f\n",
      "04\n"
     ]
    }
   ],
   "source": [
    "x = 15\n",
    "print(format(x, '02x'))\n",
    "x = 4\n",
    "print(format(x, '02x'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是字符串\n",
      "是数字\n"
     ]
    }
   ],
   "source": [
    "v = 'string'\n",
    "t = 12\n",
    "\n",
    "try:\n",
    "    int(v)\n",
    "except:\n",
    "    print('是字符串')\n",
    "\n",
    "\n",
    "try:\n",
    "    int(t)\n",
    "    print('是数字')\n",
    "except:\n",
    "    print('是字符串')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "def multiple(m, n):\n",
    "    return True if m % n == 0 else False\n",
    "\n",
    "print(multiple(20, 5))\n",
    "print(multiple(7, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is a tuple\n"
     ]
    }
   ],
   "source": [
    "#x = ['a', 'b', 'c', 'd']\n",
    "#x = {'a', 'b', 'c', 'd'}\n",
    "x = ('tuple', False, 3.2, 1)\n",
    "if type(x) is list:\n",
    "    print('x is a list')\n",
    "elif type(x) is set:\n",
    "    print('x is a set')\n",
    "elif type(x) is tuple:\n",
    "    print('x is a tuple')    \n",
    "else:\n",
    "    print('都不是.')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(75, -5)\n"
     ]
    }
   ],
   "source": [
    "def max_min(data):\n",
    "  l = data[0]\n",
    "  s = data[0]\n",
    "  for num in data:\n",
    "    if num> l:\n",
    "      l = num\n",
    "    elif num< s:\n",
    "        s = num\n",
    "  return l, s\n",
    "\n",
    "print(max_min([0, 10, 15, 40, -5, 42, 17, 28, 75]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "QQ群：428335755"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
